package com.study.d012Components;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCharacterCombination;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.Mnemonic;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * Button按钮的使用
 *
 */
public class ButtonMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		Group group = new Group();

		Button b1=new Button("测试");
		b1.setPrefSize(100, 100);

		//通过代码设置背景颜色
		/*BackgroundFill fill = new BackgroundFill(Paint.valueOf("#8fbc8f"), new CornerRadii(20), new Insets(10));
		Background background = new Background(fill);
		b1.setBackground(background);
*/
		//通过代码设置字体
		/*b1.setFont(Font.font("sans-serif", 20));*/

		//通过代码设置字体颜色   注意：颜色后面加两位代表透明度 #ffa50099 则 99代码透明度
		/*b1.setTextFill(Paint.valueOf("#ffa500"));*/



		//通过css样式来设置
		b1.setStyle("-fx-font-family:sans-serif; -fx-font-size:10;-fx-background-color: red;");


		//Button按钮单击事件
		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Button button = (Button)event.getSource();
				System.out.println(button.getText());
				System.out.println("你点击了点击按钮");
			}
		});


		//Button按钮双击事件
		b1.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				/**
				 * event.getButton().name()
				 * 		--PRIMARY ：左键
				 * 		--SECONDARY： 右键
				 * 		--MIDDLE   :  中间键
				 */
				//System.out.println(event.getButton().name());

				if(MouseButton.PRIMARY.name().equals(event.getButton().name())){
					//如果是左键
					if(event.getClickCount()==2){
						//如果双击两次

					}
				}
				System.out.println("=====");
			}
		});

		//检测键盘按键(鼠标按下)
		b1.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("你按下的是："+event.getCode().name());
				if(KeyCode.ENTER.name().equals(event.getCode().name())){
					System.out.println("你按下了Enter键.");
				}
			}

		});






		group.getChildren().add(b1);
		Scene scene = new Scene(group);

		//添加快捷键 方式一：   A + Ctrl + Shift   (Win10下测试无效)
		KeyCombination key1 = new KeyCodeCombination(KeyCode.C, KeyCombination.SHIFT_DOWN);
		Mnemonic mnemonic1 = new Mnemonic(b1, key1);
		scene.addMnemonic(mnemonic1);

		//添加快捷键 方式二：(Win10下测试无效)
		KeyCombination key2 =new KeyCharacterCombination("E",KeyCombination.SHIFT_DOWN);
		Mnemonic mnemonic2=new Mnemonic(b1, key2);
		scene.addMnemonic(mnemonic2);

		//添加快捷键  方式四  平台无关   (Win10下测试通过,但是不会触发按钮的点击事件，需要将按钮的点击事件单独分离出来或者使用b1.fire()方法调用)
		KeyCombination key4 = new KeyCodeCombination(KeyCode.D, KeyCombination.SHIFT_DOWN);
		scene.getAccelerators().put(key4, new Runnable() {
			@Override
			public void run() {
				System.out.println("执行run()方法.");
				b1.fire();  //调用b1的点击事件
			}
		});

		//添加快捷键 方式五
		KeyCombination key5 = KeyCodeCombination.valueOf("ctrl+k");
		Mnemonic mnemonic5=new Mnemonic(b1, key5);
		scene.addMnemonic(mnemonic5);


		stage.setScene(scene);

		stage.setWidth(500);
		stage.setHeight(400);
		stage.show();
	}
}
